import 'package:app/pages/home_module/teams_module/create_team_page.dart';
import 'package:app/pages/home_module/teams_module/join_team_page.dart';
import 'package:app/pages/home_module/teams_module/my_team_model.dart';
import 'package:app/pages/userinfo_module/userinfo_model.dart';
import 'package:app/pages/userinfo_module/userinfo_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:app/services/address.dart';
import 'package:app/services/dio_manager.dart';

import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../common/app_theme.dart';
import '../../common/colors.dart';
import '../../utils/hexcolor.dart';
import '../../utils/persisten_storage.dart';

class SmartDrawer extends StatefulWidget {
  const SmartDrawer({Key? key}) : super(key: key);

  @override
  State<SmartDrawer> createState() => _SmartDrawerState();
}

class _SmartDrawerState extends State<SmartDrawer> {

  MyTeamModel? _myTeamModel;

  UserinfoModel? _userinfoModel;

  /// 我的团队
  requestDataWithMyTeams() async {
    var params = {'search': ''};

    var json = await DioManager()
        .kkRequest(Address.mineTeams, method: 'get', params: params);

    MyTeamModel _model = MyTeamModel.fromJson(json);

    _myTeamModel = _model;

    setState(() {});
  }

  /// 获取用户个人信息
  requestDataWithUserInfo() async {

    var json = await DioManager().kkRequest(Address.infoRead,method: 'get');

    UserinfoModel model = UserinfoModel.fromJson(json);

    _userinfoModel = model;
    setState(() {

    });
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    requestDataWithUserInfo();
    requestDataWithMyTeams();
    // getInit();
  }

  // var name;
  //
  // /// 获取用户名称
  // getInit() async {
  //   name = await PersistentStorage().getStorage('name');
  //   setState(() {});
  // }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: Get.width - 80,
      color: Colors.white,
      child: Column(
        children: [
         GestureDetector(
           onTap: (){
            Get.toNamed(AppRoutes.userInfoView);
           },
           child:  Container(
               padding: EdgeInsets.only(top: 50, left: 15, right: 15),
               width: Get.width,
               height: 130,
               color: Colors.white,
               child: Row(
                 mainAxisAlignment: MainAxisAlignment.spaceBetween,
                 children: [
                   Row(
                     children: [
                       CircleAvatar(
                         backgroundImage: NetworkImage(
                           '${Address.homeHost}${_userinfoModel?.data?.image_128}'
                         ),
                         // child: ExtendedImage.network('${Address.homeHost}${_userinfoModel?.data?.image_128}'),
                       ),
                       Container(
                         margin: EdgeInsets.only(
                           left: 10,
                         ),
                         alignment: Alignment.center,
                         // color: Colors.yellow,
                         child: Column(
                           crossAxisAlignment: CrossAxisAlignment.start,
                           mainAxisAlignment: MainAxisAlignment.center,
                           children: [
                             Container(
                               child: Text(
                                 '${_userinfoModel?.data?.name??''}',
                                 style: TextStyle(color: AppColor.textColor),
                                 maxLines: 1,
                               ),
                             ),
                             SizedBox(
                               height: 5,
                             ),
                             // Container(
                             //   width: 100,
                             //   child: Text(
                             //     'kk科技有限公司11111111',
                             //     style: appThemeData.textTheme.subtitle1,
                             //     maxLines: 1,
                             //   ),
                             // )
                           ],
                         ),
                       ),
                     ],
                   ),
                   Icon(
                     Icons.arrow_forward_ios,
                     size: 15,
                     color: Colors.grey,
                   ),
                 ],
               )),
         ),
          Expanded(
              child: ListView.builder(
            padding: EdgeInsets.all(0),
            itemBuilder: (BuildContext context, int index) {
              return GestureDetector(
                child: Container(
                  height: 55,
                  margin: EdgeInsets.only(top: 1),
                  padding: EdgeInsets.only(left: 15, right: 15),
                  color: HexColor('#f7f7f7'),
                  alignment: Alignment.centerLeft,
                  width: Get.width,
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Text(
                            '${_myTeamModel?.data?[index].name}',
                            style: appThemeData.textTheme.bodyText1,
                          ),
                          _myTeamModel?.data?[index].is_join == true
                              ? Icon(
                                  Icons.check,
                                  size: 15,
                                )
                              : Container()
                        ],
                      )
                    ],
                  ),
                ),
                onTap: () async {
                  await PersistentStorage()
                      .setStorage('team_id', _myTeamModel?.data?[index].id);
                  BotToast.showText(text: '选择团队成功');
                },
              );
            },
            itemCount: _myTeamModel?.data?.length ?? 0,
          )),
          Container(
            height: 0.5,
            color: AppColor.lineColor,
          ),
          Container(
              color: Colors.white,
              height: 150,
              width: Get.width,
              child: Column(
                children: [
                  GestureDetector(
                    onTap: () async {
                      var data = await Get.to(CreateTeamPage());
                      if (data == 'refresh') {
                        requestDataWithMyTeams();
                      }
                    },
                    child: Container(
                      padding: EdgeInsets.only(left: 15, right: 15),
                      alignment: Alignment.centerLeft,
                      height: 45,
                      color: Colors.white,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Image.asset(
                                'images/teams.png',
                                width: 18,
                                height: 18,
                              ),
                              SizedBox(
                                width: 5,
                              ),
                              Text('创建团队',
                                  style: appThemeData.textTheme.bodyText1),
                            ],
                          ),
                          Icon(
                            Icons.arrow_forward_ios,
                            size: 15,
                            color: Colors.grey,
                          )
                        ],
                      ),
                    ),
                  ),
                  GestureDetector(
                    onTap: () async{
                     var data = await Get.to(JoinTeamPage());
                     if(data == 'refresh'){
                       requestDataWithMyTeams();
                     }
                    },
                    child: Container(
                      padding: EdgeInsets.only(left: 15, right: 15),
                      alignment: Alignment.centerLeft,
                      height: 45,
                      color: Colors.white,
                      child: Row(
                        crossAxisAlignment: CrossAxisAlignment.center,
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Row(
                            children: [
                              Image.asset(
                                'images/join_team_icon.png',
                                width: 18,
                                height: 18,
                              ),
                              SizedBox(
                                width: 5,
                              ),
                              Text('加入团队',
                                  style: appThemeData.textTheme.bodyText1),
                            ],
                          ),
                          Icon(
                            Icons.arrow_forward_ios,
                            size: 15,
                            color: Colors.grey,
                          )
                        ],
                      ),
                    ),
                  ),
                ],
              ))
        ],
      ),
    );
  }
}
